package com.ruoyi.zsyh.controller.applet.after.factory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.AuthBaiduUtils;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.ISysDtAreaService;
import com.ruoyi.zsyh.service.IZsyhAfterFactoryAddressService;
import com.ruoyi.zsyh.service.IZsyhUserAddressService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 用户地址管理Controller
 * 
 * @author zhangkm
 * @date 2022-2-21
 */
@Controller
@RequestMapping("/applet/zsyh/afterFactoryAddress")
public class ZsyhAfterFactoryAddressAppletController extends BaseController
{


    @Autowired
    private IZsyhAfterFactoryAddressService zsyhAfterFactoryAddressService;

    @Autowired
    private ISysDtAreaService sysDtAreaService;

    @Autowired
    private RedisUtil redisUtils;



    /**
     * 添加收货地址
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @RequestMapping("/saveFactoryAddress")
    @ResponseBody
    public AjaxResult saveFactoryAddress(String factoryId, String addressName, String addressPhone,
                                         String addressProvince, String addressPrivinceName,String addressCity, String addressCityName,
                                         String addressDistrict, String addressDistrictName, String addressDetail)throws Exception
    {
        SysDtArea areaProvince = sysDtAreaService.selectSysDtAreaById(addressProvince);
        SysDtArea areaCity = sysDtAreaService.selectSysDtAreaById(addressCity);
        SysDtArea areaDistrict = sysDtAreaService.selectSysDtAreaById(addressDistrict);
        ZsyhAfterFactoryAddress factoryAddress = new ZsyhAfterFactoryAddress();
        factoryAddress.setId(IdWorker.getId());
        factoryAddress.setFactoryId(factoryId);
        factoryAddress.setAddressDetail(addressDetail);
        factoryAddress.setAddressProvince(addressProvince);
        factoryAddress.setAddressPrivinceName(areaProvince.getAreaName());
        factoryAddress.setAddressCity(addressCity);
        factoryAddress.setAddressCityName(areaCity.getAreaName());
        factoryAddress.setAddressDistrict(addressDistrict);
        factoryAddress.setAddressDistrictName(areaDistrict.getAreaName());
        factoryAddress.setAddressName(addressName);
        factoryAddress.setAddressPhone(addressPhone);

        ZsyhAfterFactoryAddress factoryAddress1 = new ZsyhAfterFactoryAddress();
        factoryAddress1.setFactoryId(factoryId);
        List<ZsyhAfterFactoryAddress> list = zsyhAfterFactoryAddressService.selectZsyhAfterFactoryAddressList(factoryAddress1);
        if(list.size() == 0){
            factoryAddress.setIsDefault("1");
        }
        zsyhAfterFactoryAddressService.insertZsyhAfterFactoryAddress(factoryAddress);
        return success("操作成功");
    }

    /**
     *  编辑收货地址
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateFactoryAddress")
    @ResponseBody
    public AjaxResult updateFactoryAddress(String id,String addressName, String addressPhone,
                                           String addressProvince, String addressPrivinceName,String addressCity, String addressCityName,
                                           String addressDistrict, String addressDistrictName, String addressDetail)throws Exception
    {
        SysDtArea areaProvince = sysDtAreaService.selectSysDtAreaById(addressProvince);
        SysDtArea areaCity = sysDtAreaService.selectSysDtAreaById(addressCity);
        SysDtArea areaDistrict = sysDtAreaService.selectSysDtAreaById(addressDistrict);

        ZsyhAfterFactoryAddress factoryAddress = new ZsyhAfterFactoryAddress();
        factoryAddress.setId(id);
        factoryAddress.setAddressDetail(addressDetail);
        factoryAddress.setAddressProvince(addressProvince);
        factoryAddress.setAddressPrivinceName(areaProvince.getAreaName());
        factoryAddress.setAddressCity(addressCity);
        factoryAddress.setAddressCityName(areaCity.getAreaName());
        factoryAddress.setAddressDistrict(addressDistrict);
        factoryAddress.setAddressDistrictName(areaDistrict.getAreaName());
        factoryAddress.setAddressName(addressName);
        factoryAddress.setAddressPhone(addressPhone);
        zsyhAfterFactoryAddressService.updateZsyhAfterFactoryAddress(factoryAddress);

        return success("操作成功");
    }

    /**
     *  设为默认
     * @return
     * @throws Exception
     */
    @RequestMapping("/setDefaultFactoryAddress")
    @ResponseBody
    public AjaxResult setDefaultFactoryAddress(String factoryId, String id)throws Exception
    {
        zsyhAfterFactoryAddressService.updateSetDefaultFactoryAddress(factoryId, id);
        return success("操作成功");
    }

    /**
     * 删除收货地址
     * @return
     * @throws Exception
     */
    @RequestMapping("/delFactoryAddress")
    @ResponseBody
    public AjaxResult delFactoryAddress(String factoryId, String id)throws Exception
    {
        ZsyhAfterFactoryAddress factoryAddress = zsyhAfterFactoryAddressService.selectZsyhAfterFactoryAddressById(id);
        if("0".equals(factoryAddress.getIsDefault())){
            zsyhAfterFactoryAddressService.deleteZsyhAfterFactoryAddressById(id);
        }else{
            ZsyhAfterFactoryAddress factoryAddress1 = new ZsyhAfterFactoryAddress();
            factoryAddress1.setFactoryId(factoryId);
            List<ZsyhAfterFactoryAddress> list = zsyhAfterFactoryAddressService.selectZsyhAfterFactoryAddressList(factoryAddress1);
            if(list.size() == 1){
                return AjaxResult.error("操作失败，唯一地址无法删除");
            }else {
                String newId = "";
                for(ZsyhAfterFactoryAddress a:list){
                    if(!id.equals(a.getId())){
                        newId = a.getId();
                    }
                }
                zsyhAfterFactoryAddressService.deleteZsyhAfterFactoryAddressById(id);//删除地址
                ZsyhAfterFactoryAddress _address = new ZsyhAfterFactoryAddress();
                _address.setId(newId);
                _address.setIsDefault("1");
                zsyhAfterFactoryAddressService.updateZsyhAfterFactoryAddress(_address);//修改第一个地址为默认地址
            }
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 厂家 获取所有账户列表
     * @return
     */
    @RequestMapping("/getFactoryAddressList")
    @ResponseBody
    public AjaxResult getFactoryAddressList(String pageNum, String pageSize, String factoryId)throws Exception
    {
        ZsyhAfterFactoryAddress factoryAddress = new ZsyhAfterFactoryAddress();
        factoryAddress.setFactoryId(factoryId);
        List<ZsyhAfterFactoryAddress> listSize = zsyhAfterFactoryAddressService.selectZsyhAfterFactoryAddressList(factoryAddress);
        int total = listSize.size();
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        factoryAddress.setParams(map);
        List<ZsyhAfterFactoryAddress> list = zsyhAfterFactoryAddressService.selectZsyhAfterFactoryAddressList(factoryAddress);

        return success("成功", list, total);
    }

    /**
     * 获取默认地址
     * @param factoryId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getFactoryAddressDefault")
    @ResponseBody
    public AjaxResult getFactoryAddressDefault(String factoryId)throws Exception
    {
        ZsyhAfterFactoryAddress factoryAddress = new ZsyhAfterFactoryAddress();
        factoryAddress.setFactoryId(factoryId);
        factoryAddress.setIsDefault("1");
        List<ZsyhAfterFactoryAddress> list = zsyhAfterFactoryAddressService.selectZsyhAfterFactoryAddressList(factoryAddress);
        if(list.size() > 0){
            return success("成功", list.get(0));
        }else{
            return error("暂无返件地址");
        }

    }


}
